package com.common.event.queue;

import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.common.event.EventMessage;
import com.common.event.listener.EventMessageListenerRouter;

/**
 * 
* @Title: QueueWorker.java 
* @Package com.common.event.queue
* @Description: 队列工作线程
* @author tangzhibin
* @date 2017年3月22日 
* @version V1.0
 */
public class QueueWorker  implements Callable<Void>  {
	
	private static final Logger	LOGGER	= LoggerFactory.getLogger(QueueWorker.class);
	
	final EventMessageListenerRouter router;
	final Queue queue;
	
	protected	QueueWorker(EventMessageListenerRouter router,Queue queue){
		this.router=router;
		this.queue=queue;
	}
	
	@Override
	public Void call() {
		while (true) {
			try {
				EventMessage queueMsg = queue.popQueue();
				LOGGER.debug("pop the gprs data from  queue {}  ",JSON.toJSONString(queueMsg));
				router.doEventMessage(queueMsg);
			} catch (Exception e) {
				e.printStackTrace();
				LOGGER.error("consume  queue of  data error", e);
			}
		}
	}

}
